package com.wecloud.core.data.config;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;

import com.wecloud.core.exception.WeException;

@Component
public abstract class BussinessClient {
	
	public Logger logger = LoggerFactory.getLogger(BussinessClient.class);
	
	@Autowired
	private JPAClient jpaClient;
	
	/**
	 * sqlsession
	 * @return
	 */
	public SqlSession getJpaClient() {
		return jpaClient.getSqlSession();
	}
	/**
	 * 查询
	 * @param sql
	 * @param params
	 * @return
	 * @throws WeException
	 */
	public List<Map<String,Object>> queryForList(String sql,Map<String,Object> params) throws WeException {
		try {
			return getJpaClient().selectList(sql,params);
		} catch (Exception e) {
			logger.error("执行查询时出现异常；sql:{"+sql+"}", e);
			throw new WeException(e);
		}
	}
	/**
	 * 查询
	 * @param sql
	 * @param params
	 * @return
	 * @throws WeException
	 */
	public Map<String,Object> queryForObject(String sql,Map<String,Object> params) throws WeException {
		try {
			return getJpaClient().selectOne(sql,params);
		} catch (Exception e) {
			logger.error("执行查询时出现异常；sql:{"+sql+"}", e);
			throw new WeException(e);
		}
	}
	/**
	 * 查询
	 * @param sql
	 * @param params
	 * @return
	 * @throws WeException
	 */
	public Map<String,Object> queryForObject(String sql,String params) throws WeException {
		try {
			return getJpaClient().selectOne(sql,params);
		} catch (Exception e) {
			logger.error("执行查询时出现异常；sql:{"+sql+"}", e);
			throw new WeException(e);
		}
	}
	/**
	 * 插入
	 * @param sql
	 * @param params
	 * @return
	 * @throws WeException
	 */
	public Integer insert(String sql,Map<String,Object> params) throws WeException {
		try {
			return getJpaClient().insert(sql,params);
		} catch (Exception e) {
			getJpaClient().rollback();
			logger.error("执行查询时出现异常；sql:{"+sql+"}", e);
			throw new WeException(e);
		}
	}
	/**
	 * 更新
	 * @param sql
	 * @param params
	 * @return
	 * @throws WeException
	 */
	public Integer update(String sql,Map<String,Object> params) throws WeException {
		try {
			return getJpaClient().update(sql,params);
		} catch (Exception e) {
			getJpaClient().rollback();
			logger.error("执行查询时出现异常；sql:{"+sql+"}", e);
			throw new WeException(e);
		}
	}
	/**
	 * 删除
	 * @param sql
	 * @param params
	 * @return
	 * @throws WeException
	 */
	public Integer delete(String sql,Map<String,Object> params) throws WeException {
		try {
			return getJpaClient().delete(sql,params);
		} catch (Exception e) {
			getJpaClient().rollback();
			logger.error("执行查询时出现异常；sql:{"+sql+"}", e);
			throw new WeException(e);
		}
	}
	public PlatformTransactionManager getAnnotationDrivenTransactionManager() {
		return jpaClient.annotationDrivenTransactionManager();
	}
	public  TransactionStatus getTransactionStatus(){
        return jpaClient.getTransactionStatus();
	}
}
